﻿package mortal.game.view.shop
{
    import Message.Game.*;
    import com.gengine.global.*;
    import com.greensock.layout.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.map3D.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.shopMall.data.*;
    import mortal.game.view.shopMall.view.*;
    import mortal.mvc.core.*;

    public class BuyItemWin extends SmallWindow
    {
        protected var _shopItem:ShopSellItem;
        private var _itemName:GTextFiled;
        private var _price:GTextFiled;
        private var _totalPrice:GTextFiled;
        private var _notEnougthMoney:GTextFiled;
        private var _ownMoney:GTextFiled;
        private var _capcity:GTextFiled;
        protected var _btnBuy:GButton;
        protected var _numInput:GNumericStepper;
        private var _moneyIcon:GBitmap;
        private var _totalMoneyIcon:GBitmap;
        private var _ownMoneyIcon:GBitmap;
        private var _shopItemData:ShopItemData;
        protected var _npcId:int;
        private static var _instance:BuyItemWin;

        public function BuyItemWin()
        {
            this.setSize(270, 210);
            paddingBottom = 55;
            this.layer = LayerManager.fullScreenTopLayer;
            this.title = Language.getString(35541);
            return;
        }// end function

        public function showWin(param1:ShopItemData, param2:int = 0) : void
        {
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            this._shopItemData = param1;
            this._npcId = param2;
            if (this.isHide)
            {
                _loc_3 = Global.stage.mouseX - 241;
                _loc_4 = Global.stage.mouseY - 110;
                if (_loc_3 < 0)
                {
                    _loc_3 = 0;
                }
                else if (_loc_3 + this.width > SceneRange.display.width)
                {
                    _loc_3 = SceneRange.display.width - this.width;
                }
                if (_loc_4 < 0)
                {
                    _loc_4 = 0;
                }
                else if (_loc_4 + this.height > SceneRange.display.height)
                {
                    _loc_4 = SceneRange.display.height - this.height;
                }
                this.show(_loc_3, _loc_4);
            }
            this.setInfo();
            this.updateSize();
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(30075), 109, 68, 40, 24, this));
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(30078), 109, 122, 60, 24, this, GlobalStyle.textFormatHuang));
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(30077), 45, 192, 60, 24, this, GlobalStyle.textFormatItemGreen));
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(30076), 45, 172, 60, 24, this, GlobalStyle.textFormatItemGreen));
            this._shopItem = UICompomentPool.getUICompoment(ShopSellItem);
            this._shopItem.x = 26;
            this._shopItem.y = 46;
            var _loc_2:Boolean = false;
            this._shopItem.isDropAble = false;
            this._shopItem.isDragAble = _loc_2;
            this._shopItem.setItemStyle(ItemStyleConst.Big, ImagesConst.ShopItemBg, 6, 6);
            this.addChild(this._shopItem);
            var _loc_1:* = GlobalStyle.textFormatPutong;
            _loc_1.size = 15;
            this._itemName = UIFactory.gTextField("", 109, 44, 150, 25, this, _loc_1, true);
            _loc_1 = GlobalStyle.textFormatBai;
            _loc_1.align = AlignMode.RIGHT;
            this._price = UIFactory.gTextField("", 140, 69, 51, 20, this, _loc_1, true);
            _loc_1 = GlobalStyle.textFormatHuang;
            _loc_1.align = AlignMode.RIGHT;
            this._totalPrice = UIFactory.gTextField("", 140, 123, 51, 20, this, _loc_1, true);
            this._notEnougthMoney = UIFactory.gTextField("", 210, 123, 80, 20, this, GlobalStyle.textFormatHong);
            _loc_1 = GlobalStyle.textFormatItemGreen;
            _loc_1.align = AlignMode.RIGHT;
            this._ownMoney = UIFactory.gTextField("", 100, 172, 80, 20, this, _loc_1, true);
            this._capcity = UIFactory.gTextField("", 135, 192, 60, 20, this, _loc_1, true);
            this._btnBuy = UIFactory.gButton("购买", 211, 97, 49, 23, this);
            this._btnBuy.configEventListener(MouseEvent.CLICK, this.clickHandler);
            this._moneyIcon = UIFactory.gBitmap("", 190, 68, this);
            this._totalMoneyIcon = UIFactory.gBitmap("", 190, 121, this);
            this._ownMoneyIcon = UIFactory.gBitmap("", 180, 172, this);
            _loc_1 = GlobalStyle.textFormatBai;
            _loc_1.align = TextFormatAlign.CENTER;
            this._numInput = UIFactory.gNumericStepper(110, 96, 50, 20, this, 99, 1, "NumericStepper", _loc_1, GNumericStepper.SetMaxNum);
            this._numInput.value = 1;
            this._numInput.configEventListener(Event.CHANGE, this.changeNum);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._shopItem.dispose(param1);
            this._itemName.dispose(param1);
            this._price.dispose(param1);
            this._totalPrice.dispose(param1);
            this._ownMoney.dispose(param1);
            this._capcity.dispose(param1);
            this._btnBuy.dispose(param1);
            this._numInput.dispose(param1);
            this._moneyIcon.dispose(param1);
            this._totalMoneyIcon.dispose(param1);
            this._ownMoneyIcon.dispose(param1);
            this._notEnougthMoney.dispose(param1);
            this._shopItem = null;
            this._itemName = null;
            this._price = null;
            this._totalPrice = null;
            this._ownMoney = null;
            this._capcity = null;
            this._btnBuy = null;
            this._numInput = null;
            this._moneyIcon = null;
            this._totalMoneyIcon = null;
            this._ownMoneyIcon = null;
            this._notEnougthMoney = null;
            this._shopItemData = null;
            super.disposeImpl(param1);
            return;
        }// end function

        override protected function configParams() : void
        {
            super.configParams();
            blurTop = 10;
            paddingBottom = 50;
            return;
        }// end function

        private function setMoneyInfo(param1:Object = null) : void
        {
            var _loc_2:* = ShopConfig.instance.getTShopByCode(this._shopItemData.tShopSell.shopCode).unit;
            var _loc_3:* = Cache.instance.role.money;
            var _loc_4:* = Cache.instance.role.getMoneyByType(_loc_2);
            this._ownMoney.text = _loc_4.toString();
            this.changeNum();
            return;
        }// end function

        private function getMoneyIcon() : BitmapData
        {
            var _loc_1:* = ShopConfig.instance.getTShopByCode(this._shopItemData.tShopSell.shopCode).unit;
            var _loc_2:* = GlobalClass.getBitmapData(GameDefConfig.instance.getEPrictUnitImg(_loc_1));
            return _loc_2;
        }// end function

        private function setInfo() : void
        {
            this._shopItem.shopItemData = this._shopItemData;
            this._itemName.htmlText = ItemsUtil.getItemName(this._shopItem.itemData);
            this._totalPrice.text = this._shopItemData.tShopSell.price.toString();
            this.updateCapcity();
            this.setMoneyInfo();
            var _loc_1:* = this.getMoneyIcon();
            this._moneyIcon.bitmapData = this.getMoneyIcon();
            var _loc_1:* = _loc_1;
            this._totalMoneyIcon.bitmapData = _loc_1;
            this._ownMoneyIcon.bitmapData = _loc_1;
            if (this._shopItemData.tShopSell.activeOffer != 0)
            {
                this._price.text = this._shopItemData.tShopSell.activeOffer.toString();
            }
            else
            {
                this._price.text = this._shopItemData.tShopSell.offer.toString();
            }
            this._numInput.value = 1;
            this.changeNum();
            return;
        }// end function

        protected function changeNum(event:Event = null) : void
        {
            var _loc_2:GTextFormat = null;
            this._totalPrice.text = String(int(this._price.text) * this._numInput.value);
            if (!Cache.instance.role.enoughMoney(ShopConfig.instance.getTShopByCode(this._shopItemData.tShopSell.shopCode).unit, int(this._totalPrice.text)))
            {
                _loc_2 = GlobalStyle.textFormatHong;
                _loc_2.align = AlignMode.RIGHT;
                this._totalPrice.setTextFormat(_loc_2);
                this._notEnougthMoney.text = Language.getString(30079);
            }
            else
            {
                _loc_2 = GlobalStyle.textFormatHuang;
                _loc_2.align = AlignMode.RIGHT;
                this._totalPrice.setTextFormat(_loc_2);
                this._notEnougthMoney.text = "";
            }
            return;
        }// end function

        protected function clickHandler(event:MouseEvent) : void
        {
            var _loc_2:int = 0;
            if (event.target == this._btnBuy)
            {
                _loc_2 = this._numInput.value;
                this._shopItem.shopItemData.num = _loc_2;
                this._shopItem.shopItemData.npcId = this._npcId;
                this._shopItem.shopItemData.type = this._shopItem.shopItemData.tShopSell.isBindMoney;
                Dispatcher.dispatchEvent(new DataEvent(EventName.BuyItem, this._shopItem));
            }
            return;
        }// end function

        private function updateCapcity(param1:Object = null) : void
        {
            this._capcity.text = String(Cache.instance.pack.backPackCache.capacity - Cache.instance.pack.backPackCache.itemLength) + Language.getString(35542);
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            super.show(param1, param2);
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.setMoneyInfo);
            NetDispatcher.addCmdListener(ServerCommand.UpdateCapacity, this.updateCapcity);
            return;
        }// end function

        override public function hide() : void
        {
            super.hide();
            NetDispatcher.removeCmdListener(ServerCommand.MoneyUpdate, this.setMoneyInfo);
            NetDispatcher.removeCmdListener(ServerCommand.UpdateCapacity, this.updateCapcity);
            return;
        }// end function

        public function set useNum(param1:int) : void
        {
            this._shopItem.shopItemData.useNum = param1;
            return;
        }// end function

        public function get shopData() : ShopItemData
        {
            return this._shopItemData;
        }// end function

        public function setDefaultBuyNum(param1:int) : void
        {
            this._numInput.value = param1;
            this.changeNum();
            return;
        }// end function

        public static function get isViewShow() : Boolean
        {
            return _instance && !_instance.isHide;
        }// end function

        public static function get instance() : BuyItemWin
        {
            if (_instance == null)
            {
                _instance = new BuyItemWin;
            }
            return _instance;
        }// end function

    }
}
